//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/dao/practice_dao.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/ienum/practice/ct_enum.dart';
import 'package:hktk_app/ienum/practice/one_do_source_enum.dart';
import 'package:hktk_app/ienum/question/first_type_enum.dart';
import 'package:hktk_app/ienum/question/question_type_enum.dart';
import 'package:hktk_app/model/practice/practice_question_model.dart';
import 'package:hktk_app/model/question/paper_data_model.dart';
import 'package:hktk_app/model/question/question_data_model.dart';
import 'package:hktk_app/pages/question/child/question_exam_page.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/util/my_util.dart';

/// 单个试题做题页面
class SingleQuestionPage extends StatefulWidget {
  /// 集合id，章节/试卷
  final String collectId;

  /// 练习显示名称
  final String paperName;

  /// 试题id
  final String qId;

  /// 试题类型
  final FirstTypeEnum qType;

  /// 做题页面的类型
  final CtEnum rType;

  /// 是否显示做题记录
  final bool view;

  final OneDoSourceEnum oneDoSourceEnum;

  const SingleQuestionPage({
    super.key,
    required this.collectId,
    required this.paperName,
    required this.qId,
    required this.qType,
    required this.rType,
    this.view = true,
    this.oneDoSourceEnum = OneDoSourceEnum.erveryDay,
  });

  @override
  State<SingleQuestionPage> createState() => _SingleQuestionPageState();
}

class _SingleQuestionPageState extends State<SingleQuestionPage> {
  @override
  Widget build(BuildContext context) {
    debugPrint("singleQuestionPage 页面 -> build 方法执行 ===> ");
    debugPrint("");
    return QuestionExamPage(
      paperInit: _initData,
      quesInit: _quesInit,
      type: QuestionTypeEnum.check,
    );
  }

  /// 加载试卷数据方法
  Future<Map<String, dynamic>> _initData() async {
    Map<String, dynamic> map = {};
    try {
      PaperDataModel paperModel = _getPaper();
      map["data"] = paperModel;
      map["suc"] = true;
    } catch (e) {
      debugPrint("recordQuestionPage 页面 -> _initData 方法发生异常 ===> $e");
      map = {"suc": false, "msg": "未知错误，请重试"};
    }
    return map;
  }

  /// 获取试卷相关信息方法
  PaperDataModel _getPaper() {
    List<BogInfoModel> bogList = [
      BogInfoModel(
        name: widget.qType.desc,
        type: widget.qType,
        total: 1,
        dtkList: [],
      )
    ];
    List<DtkInfoModel> dtkList = [
      DtkInfoModel(
        index: 0,
        id: widget.qId,
        type: widget.qType,
        sign: "",
        userAns: [],
      )
    ];
    return PaperDataModel(
      name: widget.paperName,
      id: widget.collectId,
      collectId: widget.collectId,
      lastIndex: 0,
      rType: widget.rType,
      dtkList: dtkList,
      bogDtkList: bogList,
      startDate: DateTime.now().millisecondsSinceEpoch,
    );
  }

  /// 加载试题数据方法
  Future<Map<String, dynamic>> _quesInit(String qId, String cid) async {
    Map<String, dynamic> map = {};
    try {
      String? collectId;
      if (cid != "") {
        collectId = cid;
      }
      PracticeQuestionModel model = await PracticeDao.clientOneDo(
        id: qId,
        oneDoSourceEnum: widget.oneDoSourceEnum,
        industryCode: dataUtilComm.dataModel.industryId,
        examId: dataUtilComm.dataModel.examId,
        categoryId: dataUtilComm.dataModel.categoryId,
        ctEnum: dataUtilComm.paperData!.rType,
        view: widget.view,
        cid: collectId,
      );
      QuestionDataModel dataModel = MyUtil.getQuestionData(model);
      dataUtilComm.paperData!.bogDtkList[0].type = dataModel.type;
      dataUtilComm.paperData!.dtkList[0].type = dataModel.type;
      map["suc"] = true;
      map["data"] = dataModel;
    } catch (e) {
      debugPrint("recordQuestionPage 页面 -> 加载试题数据方法发生异常 ===> $e");
      map = {"suc": false, "msg": "未知错误，请重试"};
      if (e is DaoException) {
        debugPrint("recordQuestionPage 页面 -> dao异常 ===> ${e.msg}");
        map["msg"] = e.msg;
      }
    }

    return map;
  }
}
